//
//  AppInlines.h
//  SmartVideo
//
//  Created by nsm on 2019/3/7.
//  Copyright © 2019 nani. All rights reserved.
//

#ifndef AppInlines_h
#define AppInlines_h

static inline UIColor *HexColor(NSString *colorStr) {
    UIColor *color = [UIColor colorWithHexString:colorStr];
    if (!color) color = UIColor.lightGrayColor;
    return color;
}

static inline UIImage *BundleImage(NSString *name) {
    NSString *fileName = [NSString stringWithFormat:@"NSMBundle.bundle/%@", name];
    return NameImage(fileName);
}

static inline UIImage *BundleTemplateImage(NSString *name) {
    return [BundleImage(name) imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
}

static inline UIImageView *ArrowImageView(UIColor *tintColor, CGFloat scale) {
    UIImageView *imageView = UIImageView.new;
    imageView.size = CGSizeMake(8*scale, 14*scale);
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    if (tintColor) {
        imageView.tintColor = tintColor;
        imageView.image = BundleImage(@"nsm_cell_indicator");
    } else {
        imageView.image = [BundleImage(@"nsm_cell_indicator") imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    }
    return imageView;
}

static inline CGPoint CGSizeCenter(CGSize size) {
    return CGPointMake(size.width/2, size.height/2);
}

static inline NSMutableAttributedString *AttributedStringFL(NSString *str,
                                                            UIFont *font,
                                                            UIColor *color) {
    NSMutableAttributedString *attrM = [[NSMutableAttributedString alloc] initWithString:SafeString(str)];
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
    paragraph.lineSpacing = 0;
    paragraph.alignment = NSTextAlignmentLeft;
    [attrM addAttributes:@{NSParagraphStyleAttributeName: paragraph,
                           NSFontAttributeName: font,
                           NSForegroundColorAttributeName: color}
                   range:NSMakeRange(0, str.length)] ;
    return attrM;
}

static inline NSMutableAttributedString *AttributedStringFR(NSString *str,
                                                            UIFont *font,
                                                            UIColor *color) {
    NSMutableAttributedString *attrM = [[NSMutableAttributedString alloc] initWithString:SafeString(str)];
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
    paragraph.lineSpacing = 0;
    paragraph.alignment = NSTextAlignmentRight;
    [attrM addAttributes:@{NSParagraphStyleAttributeName: paragraph,
                           NSFontAttributeName: font,
                           NSForegroundColorAttributeName: color}
                   range:NSMakeRange(0, str.length)] ;
    return attrM;
}


static inline NSMutableAttributedString *AttributedStringFC(NSString *str,
                                                            UIFont *font,
                                                            UIColor *color) {
    NSMutableAttributedString *attrM = [[NSMutableAttributedString alloc] initWithString:SafeString(str)];
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
    paragraph.lineSpacing = 0;
    paragraph.alignment = NSTextAlignmentCenter;
    [attrM addAttributes:@{NSParagraphStyleAttributeName: paragraph,
                           NSFontAttributeName: font,
                           NSForegroundColorAttributeName: color}
                   range:NSMakeRange(0, str.length)] ;
    return attrM;
}

static inline NSMutableAttributedString *AttributedString(NSString *preStr,
                                                          UIFont *preFont,
                                                          UIColor *preColor,
                                                          NSString *sufStr,
                                                          UIFont *sufFont,
                                                          UIColor *sufColor,
                                                          NSTextAlignment alignment,
                                                          CGFloat lineSpacing) {
    NSString *prefix = SafeString(preStr);
    NSString *suffix = SafeString(sufStr);
    NSString *str = FormatString(@"%@%@", prefix, suffix);
    NSMutableAttributedString *attrM = [[NSMutableAttributedString alloc] initWithString:str];
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
    paragraph.lineSpacing = lineSpacing;
    paragraph.alignment = alignment;
    [attrM addAttribute:NSParagraphStyleAttributeName
                  value:paragraph
                  range:NSMakeRange(0, str.length)];
    [attrM addAttributes:@{NSFontAttributeName: preFont,
                           NSForegroundColorAttributeName: preColor}
                   range:NSMakeRange(0, prefix.length)];
    [attrM addAttributes:@{NSFontAttributeName: sufFont,
                           NSForegroundColorAttributeName: sufColor}
                   range:NSMakeRange(prefix.length, suffix.length)];
    return attrM;
}


#pragma mark - UIView
static inline void ViewBorder(UIView *view,
                              CGFloat borderWidth,
                              UIColor *borderColor) {
    view.layer.borderWidth = borderWidth;
    view.layer.borderColor = borderColor.CGColor;
}

static inline void ViewCorner(UIView *view,
                              CGFloat cornerRadius) {
    view.layer.masksToBounds = YES;
    view.layer.cornerRadius = cornerRadius;
}

#pragma mark - 颜色
#define COLOR_0                 ([UIColor blackColor])
#define COLOR_1                 ([UIColor darkGrayColor])
#define COLOR_2                 ([UIColor grayColor])
#define COLOR_3                 ([UIColor lightGrayColor])
#define COLOR_4                 ([UIColor whiteColor])
#define COLOR_5                 ([UIColor clearColor])

// 主题色
#define THEME_COLOR             (_nsmConfig.themeColor)
// 导航栏颜色
#define THEME_NAV_COLOR         (_nsmConfig.navBarColor)
// 浅蓝色(类支付宝)
#define LIGHT_BLUE_COLOR        RGBColor(0, 130, 250)
// 默认cell上灰色文字颜色
#define SUB_TEXT_COLOR          (_nsmConfig.subtextColor)
// 默认字体颜色
#define PLACEHOLDER_TEXT_COLOR  (_nsmConfig.placeholderColor)
// 按钮禁用颜色
#define DISABLED_COLOR          RGBColor(220, 220, 220)
// 默认cell选中颜色
#define SELECTED_COLOR          RGBColor(220, 220, 220)
// 默认cell分割线颜色
#define SEPARATOR_COLOR         (_nsmConfig.separatorColor)
// 系统分组tableView颜色
#define GROUP_TABLECOLOR        (_nsmConfig.tableViewBackgroundColor)
// 黑色上的分割线颜色
#define THEME_LINE_COLOR        RGBColor(57, 59, 70)
// 高亮的黄色文字
#define THEME_TEXT_COLOR        RGBColor(255, 210, 33)
// 按钮背景颜色
#define THEME_BTN_BG_COLOR      RGBColor(255, 44, 85)
// 按钮背景颜色
#define MONEY_TEXT_COLOR        RGBColor(255, 165, 60)

#define kPlaceholderImage       (PlaceholderImage())
// global_avatar/mine_header
#define kPlaceholderAvatar      (NameImage(@"global_avatar"))
#define kZoomImage              NameImage(@"nsm_header_zoom")

#define kEmptyDataImage         BundleTemplateImage(@"nsm_empty_tip")
#define kEmptyDataDesc(t)       AttributedStringFC((t), CustomFont(14), COLOR_3)

#define _keyWindow              (UIApplication.sharedApplication.delegate.window)
#define kSafeAreaBottom         (_keyWindow.nsm_safeAreaInsets.bottom)

static const CGFloat kSideMargin = 15.0;
static const NSUInteger kMaxUploadImageDataLength = 700*700;

#endif /* AppInlines_h */
